# -*- coding:utf-8 -*-
'''
输入一个链表，从尾到头打印链表每个节点的值
'''


class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    # 返回从尾部到头部的列表值序列，例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        stack = []
        if listNode is None:
            return stack
        while listNode.val is not None:
            stack.append(listNode.val)
            if listNode.next is None:
                break
            else:
                listNode = listNode.next
        return stack[::-1]

    def printListFromTailToHead1(self, listNode):
        # write code here
        self.stack = []
        def travel(head):
            if not head.next: return head
            x = travel(head.next)
            self.stack.append(x.val)
            return x
        travel(listNode)
        return self.stack

    def reverse_listNode(self,node):
        """
        翻转链表
        :param node:
        :return:
        """
        if not node: return
        cur = node
        pre = None
        while cur:
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
        return pre


s = Solution()
l = ListNode(3)
l1 = ListNode(2)
l2 = ListNode(1)
l.next = l1
l1.next = l2
x = s.reverse_listNode(l)
while x:
    print(x.val)
    x = x.next

